import { Component, OnInit } from '@angular/core';
import {NzMessageService, NzModalService} from "ng-zorro-antd";
import { catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { ProductService } from '../../../service/prod/product.service';

@Component({
  selector: 'app-goods-price-modal',
  templateUrl: './goods-price-modal.component.html',
  styleUrls: ['./goods-price-modal.component.less']
})
export class GoodsPriceModalComponent implements OnInit {
    ids = []
    mode;
    data = [];
    // 修改模式
    radioValue1 = null;
    radioValue2 = null;
    radioValue3 = null;
    // 计算的算子
    calc1 = null;
    calc2 = null;
    calc3 = null;
    // 直接修改
    changeInput1 = null;
    changeInput2 = null;
    changeInput3 = null;
    // 计算方式
    selectedValue1 = '加';
    selectedValue2 = '加';
    selectedValue3 = '加';

    tabIndex = 0;
    style = {
        display   : 'inline-block',
        height    : '30px',
        lineHeight: '30px'
    };
    constructor(public msg: NzMessageService,
              private modalService: NzModalService,
              private productService: ProductService             
    ) {

    }

    ngOnInit() {
        this.loadProduct();
    }

    loadProduct() {
        this.productService.getProductByIds(this.ids.toString()).pipe(
            catchError(err => {
                return of(err);
            })
        ).subscribe(data => {
            if (data.status === 200) {
                this.data = [...data.data.rows];                
            } 
        })
    }

    changeTab(index) {
        // 使用异步的方式更新组件, 防止ngIf错误   
        Promise.resolve(null).then(() => this.tabIndex = index);
    }

    calcNewPrice(mode, calc, val) {
        if (mode === '加') {
            return +calc + +val;
        } else if (mode === '减') {
            return val - calc;
        } else if (mode === '乘') {
            if (!calc) {
                return val;
            }
            return val * calc;
        } else if (mode === '除') {
            if (calc === 0 || !calc) {
                return val;
            }
            return (val / calc).toFixed(2);
        }
    }

    getNewPrice(mode, list) {
        if (mode === 1) {
            return this.radioValue1 ? (this.radioValue1 === 'A' ? (this.changeInput1 || this.getTradePrice(list)) : (this.calcNewPrice(this.selectedValue1, this.calc1, this.getTradePrice(list)))) : this.getTradePrice(list);
        } else if (mode === 2) {
            return this.radioValue2 ? (this.radioValue2 === 'A' ? (this.changeInput2 || this.getRetailPrice(list)) : (this.calcNewPrice(this.selectedValue2, this.calc2, this.getRetailPrice(list)))) : this.getRetailPrice(list);
        } else if (mode === 3) {
            return this.radioValue3 ? (this.radioValue3 === 'A' ? (this.changeInput3 || this.getMarketPrice(list)) : (this.calcNewPrice(this.selectedValue3, this.calc3, this.getMarketPrice(list)))) : this.getMarketPrice(list);
        }
    }

    getTradePrice(list) {
        if (this.mode === 1) {
            return list.tradePrice;
        } 
        return list.tradeDPrice;
    }

    getRetailPrice(list) {
        if (this.mode === 1) {
            return list.retailPrice;
        } 
        return list.retailDPrice;
    }
    
    getMarketPrice(list) {
        if (this.mode === 1) {
            return list.marketPrice;
        } 
        return list.marketDPrice;
    }
}

